package com.vtuzx.kingdee.xqc_core.dao;

import com.vtuzx.core.db.mongo.impl.VtuzxDaoImpl;
import com.vtuzx.kingdee.xqc_core.bean.VtuzxArrayList;
import com.vtuzx.kingdee.xqc_core.constant.BillStatus;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/** 生产领料单领料Dao */
@Component
public class MaterialBomPickDetailDao extends VtuzxDaoImpl {
    /** 集合名称 */
    public static final String NAME = "t_material_bom_pick_detail";

    public MaterialBomPickDetailDao() {
        super(NAME);
    }

    /**
     * 新建单据
     *
     * @param _userId 用户ID
     * @param userName 用户名
     * @param doc 数据
     * @return 单据ID
     */
    public ObjectId insertBill(ObjectId _userId, String userName, Document doc) {
        doc.append("_createUserId", _userId);
        doc.append("createUserName", userName);
        doc.append("_updateUserId", _userId);
        doc.append("updateUserName", userName);
        return insertOne(doc);
    }

    /**
     * 审批单据
     *
     * @param _userId 用户ID
     * @param userName 用户名
     * @param billNo 数据编号
     */
    public void audit(ObjectId _userId, String userName, String billNo) {
        updateOne(Criteria.where("billNo").is(billNo),
                new Document("status", BillStatus.Audit.toString())
                        .append("auditTime", new Date())
                        .append("_auditUserId", _userId)
                        .append("auditUserName", userName));
    }

    /**
     * 反审批单据
     *
     * @param billNo 数据编号
     */
    public void unaudit(String billNo) {
        updateOne(Criteria.where("billNo").is(billNo), new Document("status", BillStatus.Save.toString()));
    }

    /**
     * 用于物料收发明细（计算所有出库数量）期初
     * @param cond
     * @return
     */
    public Iterator<Document> findOutDetail(Criteria cond) {
        List<Document> docList = new ArrayList<>();
        docList.add(new Document("$group", new Document("_id", "any").append("tableA", new Document("$push", "$$ROOT"))));
        docList.add(new Document("$lookup", new Document("from", MaterialBomPickDetailDao.NAME).append("foreignField", "xxx1").append("localField", "xxx2").append("as", MaterialBomPickDetailDao.NAME)));
        docList.add(new Document("$lookup", new Document("from", BatchAndExpDetailDao.NAME).append("foreignField", "xxx1").append("localField", "xxx2").append("as", BatchAndExpDetailDao.NAME)));
        docList.add(new Document("$lookup", new Document("from", OtherOutDetailDao.NAME).append("foreignField", "xxx1").append("localField", "xxx2").append("as", OtherOutDetailDao.NAME)));
        docList.add(new Document("$lookup", new Document("from", MaterialBomSupplementDetailDao.NAME).append("foreignField", "xxx1").append("localField", "xxx2").append("as", MaterialBomSupplementDetailDao.NAME)));
        docList.add(new Document("$lookup", new Document("from", SaleOutDetailDao.NAME).append("foreignField", "xxx1").append("localField", "xxx2").append("as", SaleOutDetailDao.NAME)));
        docList.add(new Document("$lookup", new Document("from", AllocationDetailDao.NAME).append("foreignField", "xxx1").append("localField", "xxx2").append("as", AllocationDetailDao.NAME)));
        //生产领料
        docList.add(new Document("$lookup", new Document("from", PrdPickDetailDao.NAME).append("foreignField", "xxx1").append("localField", "xxx2").append("as", PrdPickDetailDao.NAME)));
        docList.add(new Document("$project", new Document("_id", null).append("allValue", new Document("$setUnion", new VtuzxArrayList<>("$tableA", "$"+MaterialBomPickDetailDao.NAME,
                "$"+OtherOutDetailDao.NAME,
                "$" + MaterialBomSupplementDetailDao.NAME,
                "$" + BatchAndExpDetailDao.NAME,
                "$" + SaleOutDetailDao.NAME,
                "$" + AllocationDetailDao.NAME,
                "$" + PrdPickDetailDao.NAME)))));
        docList.add(new Document("$unwind", new Document("path", "$allValue")));
        docList.add(new Document("$match", cond.getCriteriaObject()));
        //docList.add(new Document("$match", cond.getCriteriaObject()));

        return this.aggregate(docList);
    }
}
